Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 9, 2026

Fix for Type.GetInterfaceMap Trimming Analyzer Issue (#117849)

Problem

The trimming analyzer fails to report diagnostics when method calls are used in field access expressions. For example, type.GetInterfaceMap(@interface).TargetMethods does not warn, while type.GetInterfaceMap(@interface) correctly reports IL2072.

Root cause: TrimAnalysisVisitor.VisitFieldReference does not visit the field's instance operation before processing the field itself.

Fix: Visit the instance before processing the field, consistent with how other visitor methods handle instances (property references, event references).

Changes

  1. TrimAnalysisVisitor.cs: Added Visit(fieldRef.Instance, state); at the start of VisitFieldReference()
  2. GetInterfaceDataFlow.cs: Added comprehensive test cases for the GetInterfaceMap scenario
    • Fixed to use typeof(TestType) instead of o.GetType() to avoid unrelated IL2075 warnings
    • Uses array indexing [0] instead of LINQ for test framework compatibility

Testing

  • ✅ Code review - no issues
  • ✅ Security scan (CodeQL) - no vulnerabilities
  • ✅ Test compilation - successful
  • ✅ Test warnings - focused on IL2072 only, no unexpected IL2075 warnings
  • ⏳ Full test suite - will be verified in CI

Impact

This is a targeted fix that only affects field reference analysis. The change ensures that:

  1. Method calls are analyzed regardless of how their return values are used
  2. Behavior is consistent across all usage patterns
  3. No false negatives in trimming diagnostics
Original prompt

This section details on the original issue you should resolve

<issue_title>Type.GetInterfaceMap(Type) is treated oddly by the trimming analyzers</issue_title>
<issue_description>### Description

The second usage of GetInterfaceMap causes

IL2072: 'interfaceType' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetInterfaceMap(Type)'. The return value of method 'System.Linq.Enumerable.First<TSource>(IEnumerable<TSource>)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

while the first usage doesn't cause any warning.

I am not sure whether it's how it's supposed to be or it's an actual bug.

Reproduction Steps

object o = new();

var type = o.GetType();

var @interface = type.GetInterfaces().First();

_ = type.GetInterfaceMap(@interface).TargetMethods;

_ = type.GetInterfaceMap(@interface);

Expected behavior

I am not sure

Actual behavior

The warning is reported or not depending on whether some field is referenced or not

Regression?

No response

Known Workarounds

No response

Configuration

.NET 8

Other information

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Jan 9, 2026
Copilot AI changed the title [WIP] Fix trimming analyzer issue with Type.GetInterfaceMap Fix trimming analyzer missing diagnostics for method calls in field access expressions Jan 9, 2026
Copilot AI requested a review from sbomer January 9, 2026 19:52
@sbomer
Copy link
Member

sbomer commented Jan 15, 2026

@copilot fix error:

'/__w/1/s/src/tools/illink/test/Mono.Linker.Tests.Cases.Expectations/Support/DynamicallyAccessedMembersAttribute.cs'.\n/__w/1/s/artifacts/bin/ILLink.testcases/linker_tests/DataFlow/GetInterfaceDataFlow/GetInterfaceDataFlow.cs(197,55): error CS1061: 'Type[]' does not contain a definition for 'First' and no accessible extension method 'First' accepting a first argument of type 'Type[]' could be found (are you missing a using directive or an assembly reference?)\n/__w/1/s/artifacts/bin/ILLink.testcases/linker_tests/DataFlow/GetInterfaceDataFlow/GetInterfaceDataFlow.cs(209,55): error CS1061: 'Type[]' does not contain a definition for 'First' and no accessible extension method 'First' accepting a first argument of type 'Type[]' could be found (are you missing a using directive or an assembly reference?)\n/__w/1/s/artifacts/bin/ILLink.testcases/linker_tests/DataFlow/GetInterfaceDataFlow/GetInterfaceDataFlow.cs(219,55): error CS1061: 'Type[]' does not contain a definition for 'First' and no accessible extension method 'First' accepting a first argument of type 'Type[]' could be found (are you missing a using directive or an assembly reference?)\n)\n---- System.Exception : Roslyn compilation

Copy link
Contributor Author

Copilot AI commented Jan 15, 2026

@copilot fix error:

'/__w/1/s/src/tools/illink/test/Mono.Linker.Tests.Cases.Expectations/Support/DynamicallyAccessedMembersAttribute.cs'.\n/__w/1/s/artifacts/bin/ILLink.testcases/linker_tests/DataF...

Fixed in 8b353ad. Replaced .First() with [0] array indexing since the test framework doesn't include LINQ assembly references. This pattern is consistent with other dataflow tests (e.g., ArrayDataFlow.cs).

@sbomer
Copy link
Member

sbomer commented Jan 15, 2026

@copilot fix this error:

Unexpected warning found: ILLink: Trim analysis warning IL2075: Mono.Linker.Tests.Cases.DataFlow.GetInterfaceDataFlow.GetInterfaceMap_DataFlow.TestDirectCall(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
Unexpected warning found: ILLink: Trim analysis warning IL2075: Mono.Linker.Tests.Cases.DataFlow.GetInterfaceDataFlow.GetInterfaceMap_DataFlow.TestFieldAccess(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
Unexpected warning found: ILLink: Trim analysis warning IL2075: Mono.Linker.Tests.Cases.DataFlow.GetInterfaceDataFlow.GetInterfaceMap_DataFlow.TestMultipleFieldAccess(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Be sure to run this test before committing.

Copy link
Contributor Author

Copilot AI commented Jan 15, 2026

@copilot fix this error:

Unexpected warning found: ILLink: Trim analysis warning IL2075: Mono.Linker.Tests.Cases.DataFlow.GetInterfaceDataFlow.GetInterfaceMap_DataFlow.TestDirectCall(): 'this' argu...

Fixed in 025264e. Replaced o.GetType() with typeof(TestType) to avoid the unrelated IL2075 warnings. The tests now focus only on the IL2072 warnings for GetInterfaceMap, which is what we're testing with this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linkable-framework Issues associated with delivering a linker friendly framework

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Type.GetInterfaceMap(Type) is treated oddly by the trimming analyzers

2 participants